package com.lswms.controller;

import com.lswms.annotation.Login;
import com.lswms.annotation.LoginUser;
import com.lswms.common.page.PageInfo;
import com.lswms.common.page.PageRequest;
import com.lswms.common.utils.Result;
import com.lswms.common.validator.ValidatorUtils;
import com.lswms.dto.vo.req.QuerySignInfoReq;
import com.lswms.dto.vo.req.SignListReq;
import com.lswms.dto.vo.req.SignReq;
import com.lswms.dto.vo.resp.LotteryInfoResp;
import com.lswms.dto.vo.resp.LotteryListResp;
import com.lswms.dto.vo.resp.QuerySignDetailResp;
import com.lswms.dto.vo.resp.SignListResp;
import com.lswms.entity.YnUserEntity;
import com.lswms.service.YnLotteryRecordService;
import com.lswms.service.YnLotteryService;
import com.lswms.service.YnSignRecordService;
import com.lswms.service.YnSignService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletRequest;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import springfox.documentation.annotations.ApiIgnore;

import com.alibaba.fastjson.JSON;

/**
 * 挑战期数表
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2020-05-06
 */
@RestController
@RequestMapping("/api/ynsign")
@Api(tags="挑战期数表")
@Slf4j
public class YnSignController {

	@Autowired
	private YnSignService ynSignService;
	
	@Autowired
	private YnLotteryService ynLotteryService;
	
	@Autowired
	private YnLotteryRecordService ynLotteryRecordService;
	
	@Autowired
	private YnSignRecordService ynSignRecordService;

	
    /**
     * 首页挑战期数列表
     * @param dto
     * @param request
     * @return
     */
    @PostMapping("querySign")
    @ApiOperation("首页挑战期数列表")
    public Result<Object> querySign(HttpServletRequest request){
        return new Result<Object>().ok(ynSignService.querySign());
    }
    
    /**
     * 抽奖信息
     * @param dto
     * @param request
     * @return
     */
    @PostMapping("lotteryInfo")
    @ApiOperation("抽奖信息")
    @Login
    public Result<LotteryInfoResp> lotteryInfo(@ApiIgnore @LoginUser YnUserEntity ynuser,HttpServletRequest request){
        return new Result<LotteryInfoResp>().ok(ynLotteryService.lotteryInfo(ynuser));
    }

    /**
     * 抽奖
     * @param dto
     * @param request
     * @return
     */
    @PostMapping("lottery")
    @ApiOperation("抽奖")
    @Login
    public Result<Object> lottery(@ApiIgnore @LoginUser YnUserEntity ynuser,HttpServletRequest request){
    	ynLotteryService.lottery(ynuser);
    	return new Result<Object>();
    }
    /**
     * 中奖记录
     * @param dto
     * @param request
     * @return
     */
    @PostMapping("lotteryList")
    @ApiOperation("中奖记录")
    @Login
    public Result<PageInfo<LotteryListResp>> lotteryList(@RequestBody PageRequest dto,@ApiIgnore @LoginUser YnUserEntity ynuser,HttpServletRequest request){
    	log.info("中奖记录，参数：dto:{},user:{}",JSON.toJSONString(dto),JSON.toJSONString(ynuser));
    	ValidatorUtils.validateEntity(dto);
    	PageInfo<LotteryListResp> resp = ynLotteryRecordService.lotteryList(dto, ynuser);
        return new Result<PageInfo<LotteryListResp>>().ok(resp);
    }

    /**
     * 挑战记录
     * @param dto
     * @param request
     * @return
     */
    @PostMapping("signList")
    @ApiOperation("挑战记录")
    @Login
    public Result<PageInfo<SignListResp>> signList(@RequestBody SignListReq dto,@ApiIgnore @LoginUser YnUserEntity ynuser,HttpServletRequest request){
    	log.info("挑战记录，参数：dto:{},user:{}",JSON.toJSONString(dto),JSON.toJSONString(ynuser));
    	ValidatorUtils.validateEntity(dto);
    	PageInfo<SignListResp> resp = ynSignRecordService.signList(dto, ynuser);
        return new Result<PageInfo<SignListResp>>().ok(resp);
    }

    /**
     * 获取挑战等级详情信息
     * @param dto
     * @param request
     * @return
     */
    @PostMapping("querySignDetail")
    @ApiOperation("获取挑战等级详情信息")
    @Login
    public Result<QuerySignDetailResp> querySignDetail(@RequestBody QuerySignInfoReq dto,@ApiIgnore @LoginUser YnUserEntity ynuser,HttpServletRequest request){
    	log.info("获取挑战等级详情信息:{}",JSON.toJSONString(dto));
    	ValidatorUtils.validateEntity(dto);
        return new Result<QuerySignDetailResp>().ok(ynSignService.querySignDetail(dto,ynuser));
    }
    
    

    /**
     * 挑战
     * @param dto
     * @param request
     * @return
     */
    @PostMapping("preSign")
    @ApiOperation("挑战")
    @Login
    public Result<Object> preSign(@RequestBody QuerySignInfoReq dto,@ApiIgnore @LoginUser YnUserEntity ynuser,HttpServletRequest request){
    	log.info("挑战:{}",JSON.toJSONString(dto));
    	ValidatorUtils.validateEntity(dto);
    	ynSignService.preSign(dto,ynuser);
    	return new Result<Object>();
    }
    /**
     * 打卡
     * @param dto
     * @param request
     * @return
     */
    @PostMapping("sign")
    @ApiOperation("打卡")
    @Login
    public Result<Object> sign(@RequestBody SignReq dto,@ApiIgnore @LoginUser YnUserEntity ynuser,HttpServletRequest request){
    	log.info("打卡:{}",JSON.toJSONString(dto));
    	ValidatorUtils.validateEntity(dto);
    	ynSignService.sign(dto,ynuser);
    	return new Result<Object>();
    }

}